<template> 
  <div class="app-container">
<!--    <el-card class="filter-container" shadow="never">-->
<!--      <div>-->
<!--        <el-button-->
<!--          style="float:right"-->
<!--          @click="handleResetSearch()"-->
<!--          size="small">-->
<!--          重置-->
<!--        </el-button>-->
<!--        <el-button-->
<!--          style="float:right;margin-right: 15px"-->
<!--          type="primary"-->
<!--          @click="handleSearchList()"-->
<!--          size="small">-->
<!--          查询搜索-->
<!--        </el-button>-->
<!--      </div>-->
<!--      <div style="margin-top: 15px">-->
<!--        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">-->
<!--          <el-form-item label="输入搜索：">-->
<!--            <el-input v-model="listQuery.keyword" class="input-width" placeholder="名称/路由" clearable></el-input>-->
<!--          </el-form-item>-->
<!--        </el-form>-->
<!--      </div>-->
<!--    </el-card>-->
<!--    <el-card class="operate-container" shadow="never">-->
<!--      <i class="el-icon-tickets"></i>-->
<!--      <span>数据列表</span>-->
<!--      <el-button size="mini" type="primary" class="btn-add" @click="handleAdd()" style="margin-left: 20px">添加</el-button>-->
<!--    </el-card>-->
    <div class="table-container">
      <el-form :inline="true" :model="listQuery" class="demo-form-inline">
        <el-form-item label="输入搜索:">
          <el-input v-model="listQuery.keyword" placeholder="名称/路由" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearchList">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            style="float:right"
            @click="handleResetSearch()"
           >
            重置
          </el-button>
        </el-form-item>

        <el-button type="primary" style="float: right;" @click="handleAdd">添加</el-button>
      </el-form>
      <el-table ref="adminTable"
                :data="list"
                v-loading="listLoading" border
                :header-cell-style="tableHeaderColor">
        <el-table-column label="页面id" width="100" align="center">
          <template slot-scope="scope">{{scope.row.pageId}}</template>
        </el-table-column>
        <el-table-column label="页面名称" align="center">
          <template slot-scope="scope">{{scope.row.name}}</template>
        </el-table-column>
        <el-table-column label="页面路由" align="center">
          <template slot-scope="scope">{{scope.row.url}}</template>
        </el-table-column>
        <el-table-column label="页面类型" align="center">
          <template slot-scope="scope">{{scope.row.type === 0? '目录': '菜单'}}</template>
        </el-table-column>
        <el-table-column label="父级页面" align="center">
          <template slot-scope="scope">{{scope.row.parentId}}</template>
        </el-table-column>
        <el-table-column label="子页面数量" align="center">
          <template slot-scope="scope">{{scope.row.type === 0? scope.row.pageCount: '--'}}</template>
        </el-table-column>
        <el-table-column label="排序" align="center">
          <template slot-scope="scope">{{scope.row.sort}}</template>
        </el-table-column>
        <el-table-column label="图标" align="center">
          <template slot-scope="scope">{{scope.row.icon}}</template>
        </el-table-column>
        <el-table-column label="状态" width="140" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleStatusChange(scope.$index, scope.row)"
              :active-value="0"
              :inactive-value="1"
              v-model="scope.row.status">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="添加时间" width="160" align="center">
          <template slot-scope="scope">{{scope.row.createDate | formatDateTime}}</template>
        </el-table-column>
        <el-table-column label="操作" width="220" align="center">
          <template slot-scope="scope">
            <el-button size="mini"
                       icon="el-icon-edit"
                       type="primary" round
                       @click="handleUpdate(scope.$index, scope.row)">
              编辑
            </el-button>
            <el-button size="mini"
                       icon="el-icon-delete"
                       type="danger" round
                       @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      :title="isEdit?'编辑页面':'添加页面'"
      :visible.sync="dialogVisible"
      width="40%">
      <el-form :model="pagesForm"
               :rules="rules"
               ref="pagesForm"
               label-width="150px" size="small">
        <el-form-item label="页面名称：" prop="name">
          <el-input v-model="pagesForm.name" style="width: 250px"></el-input>
        </el-form-item>
        <el-form-item label="页面路由：" prop="url">
          <el-input v-model="pagesForm.url" style="width: 250px"></el-input>
        </el-form-item>
        <el-form-item label="父级页面" v-show="showParent">
          <el-select v-model="pagesForm.parentId" style="width: 250px">
            <el-option label="无上级菜单" :key="0" :value="0"></el-option>
            <el-option
              v-for="item in oneLevelPages"
              :key="item.pageId"
              :label="item.name"
              :value="item.pageId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="页面类型：">
          <el-radio-group v-model="pagesForm.type" :disabled="typeIsDisabled" @change="typeChange">
            <el-radio :label="0">目录</el-radio>
            <el-radio :label="1">菜单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序：">
          <el-input v-model="pagesForm.sort" style="width: 250px"></el-input>
        </el-form-item>
        <el-form-item label="页面图标：">
          <el-input v-model="pagesForm.icon" style="width: 250px"></el-input>
          <svg-icon style="margin-left: 8px" :icon-class="pagesForm.icon"></svg-icon>
        </el-form-item>
        <el-form-item label="简介">
          <el-input type="textarea" v-model="pagesForm.description" style="width: 250px"></el-input>
        </el-form-item>
        <el-form-item label="状态：">
          <el-radio-group v-model="pagesForm.status">
            <el-radio :label="0">正常</el-radio>
            <el-radio :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirm('pagesForm')" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import {delPages, listPageOneLevel, listPages, savePages,updatePagesStatus} from '@/api/pages';
  import {formatDate} from '@/utils/date';

  // 查询对象
  const defaultListQuery = {
    pageNum: 1,
    pageSize: 10,
    keyword: null
  };

  // 默认表单对象
  const defaultPages = {
    pageId: null,
    name: null,
    description: null,
    url: null,
    parentId: 0,
    type: 0,
    sort: null,
    icon: null,
    status: 0
  };

  // 默认页面数据
  export default {
    name: 'adminList',
    data() {
      return {
        // 查询对象
        listQuery: Object.assign({}, defaultListQuery),
        // 数据列表
        list: null,
        // 分页推向
        total: null,
        listLoading: false,
        dialogVisible: false,
        // 表单对象
        pagesForm: Object.assign({}, defaultPages),
        isEdit: false,
        allocDialogVisible: false,
        showParent: false,
        typeIsDisabled: false,
        oneLevelPages: [],
        rules: {
          name: [
            {required: true, message: '请输入页面名称', trigger: 'blur'},
            {min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur'}
          ],
          url: [
            {required: true, message: '请输入页面路由', trigger: 'blur'},
            {min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur'}
          ]
        }
      }
    },
    created() {
      this.getList();
    },
    filters: {
      formatDateTime(time) {
        if (time == null || time === '') {
          return 'N/A';
        }
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      }
    },
    methods: {
      tableHeaderColor({ row, column, rowIndex, columnIndex }) {
        if (rowIndex === 0) {
          return "background-color:#efefef;color:#333";
        }
      },
      typeChange(){
        if(this.pagesForm.type === 0){
          this.showParent = false
        }else{
          this.showParent = true
        }
      },
      handleResetSearch() {
        this.listQuery = Object.assign({}, defaultListQuery);
      },
      handleSearchList() {
        this.listQuery.pageNum = 1;
        this.getList();
      },
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleAdd() {
        this.dialogVisible = true;
        this.isEdit = false;
        this.pagesForm = Object.assign({}, defaultPages);
        if(this.$refs['pagesForm'] !== undefined){
          this.$refs['pagesForm'].resetFields();
        }
        this.getOneLevelPageList(-1);
      },
      handleStatusChange(index, row) {
        this.$confirm('是否要修改该状态?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let req = {
            pageId: row.pageId,
            status: row.status
          };
          updatePagesStatus(req).then(response => {
            this.$message({
              type: 'success',
              message: '修改成功!'
            });
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消修改'
          });
          this.getList();
        });
      },
      handleDelete(index, row) {
        this.$confirm('是否要删除该菜单?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delPages(row.pageId).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList();
          });
        }).catch(() => {
        });
      },
      handleUpdate(index, row) {
        this.dialogVisible = true;
        this.isEdit = true;
        if(this.$refs['pagesForm'] !== undefined){
          this.$refs['pagesForm'].resetFields();
        }
        this.pagesForm = Object.assign({}, row);
        if(this.pagesForm.type === 0){
          this.showParent = false
          if(this.pagesForm.pageCount > 0){
            this.typeIsDisabled = true;
          }else {
            this.typeIsDisabled = false;
          }
        }else{
          this.showParent = true
          this.typeIsDisabled = false;
        }
        this.getOneLevelPageList(this.pagesForm.pageId);
      },
      handleDialogConfirm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm('是否提交数据', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              // type =0 为目录则parentId只能为 0
              if(this.pagesForm.type === 0){
                this.pagesForm.parentId = 0;
              }
              savePages(this.pagesForm).then(response => {
                this.$message({
                  message: '操作成功！',
                  type: 'success'
                });
                this.dialogVisible = false;
                this.getList();
              })
            }).catch(() => {
            });;
          }
        });
      },
      getList() {
        this.listLoading = true;
        listPages(this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.records;
          this.total = response.data.total;
        });
      },
      getOneLevelPageList(pageId) {
        listPageOneLevel(pageId).then(response => {
          this.oneLevelPages = (response.data);
        });
      }
    }
  }
</script>
<style></style>


